package com.jlsoft.config.token;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

/**
 * jwt token
 */
@Configuration
public class TokenConfig {

    public static final String signingKey = "chen";

    @Configuration
    @ConditionalOnProperty(prefix = "jlsoft.security.oauth2", name = "storeType", havingValue = "jwt", matchIfMissing = true)
    public static class JwtTokenConfig {

        @Bean
        public TokenStore jwtTokenStore () {
            return new JwtTokenStore(jwtAccessTokenConverter());
        }

        @Bean
        public JwtAccessTokenConverter jwtAccessTokenConverter () {
            JwtAccessTokenConverter accessTokenConverter = new JwtAccessTokenConverter();
            //设置签名
            accessTokenConverter.setSigningKey(signingKey);
            return accessTokenConverter;
        }


        @Bean
        @ConditionalOnMissingBean(name = "jwtTokenEnhancer")
        public TokenEnhancer jwtTokenEnhancer () {

            return new JwtTokenEnhancer();
        }

    }


}
